<div class="mini-toolbar" style="padding:0px;margin:0px;border-bottom:0;width:100%;">
	<table style="width:100%;">
		<tr>
			<td style="width:100%;">
				<a class="mini-button" iconCls="icon-addnew" id="agencyAdd" plain="true">增加直属组织机构</a>
				<a class="mini-button" iconCls="icon-edit" id="agencyEdit" plain="true">修改组织机构</a>
				<a class="mini-button" iconCls="icon-remove" id="agencyDel" plain="true">删除组织机构</a>
				<span class="separator"></span>
				<a class="mini-button" iconCls="icon-reload" id="agencyReload" plain="true">刷新</a>
			</td>
		</tr>
	</table>
</div>
<div id="treegrid1" class="mini-treegrid" style="width:100%;height:42%;" url="/common/agency" showTreeIcon="false" treeColumn="name" idField="id" parentField="parent" resultAsTree="false" allowSelect="true">
    <div property="columns">
        <div type="indexcolumn"></div>
        <div name="name" field="name" width="250px">组织机构名称</div>
        <div field="addres" width="100px">地址</div>
        <div field="contact" width="100px">负责人</div>
        <div field="businessname" width="100px">业务范围</div>
        <div field="status" width="100px">是否工单确认</div>
        <div field="tel" width="100px">电话</div>
        <div field="desc" width="100px">描述</div>
        <div field="updated" width="80px" dateFormat="yyyy-MM-dd">更新时间</div>
    </div>
</div>
<div class="mini-toolbar" style="padding:0px;margin:6px 0px 0px 0px;border-bottom:0;width:100%;">
	<table style="width:100%;">
		<tr>
			<td style="width:100%;">
				<a class="mini-button" iconCls="icon-addnew" id="buttonAdd" onclick="newRow()" plain="true" enabled="false">增加职位</a>
				<a class="mini-button" iconCls="icon-reload" id="PositionReload" plain="true">刷新</a>
			</td>
		</tr>
	</table>
</div>
<div id="grid1" class="mini-datagrid" allowAlternating="true" style="width:100%;height:45%;" showFooter="false" idField="id" allowResize="false" borderStyle="border-left:0;border-right:0;" url="/common/position" >
	<div property="columns">
		<div type="indexcolumn"></div>
		<div field="name" width="100">名称<input property="editor" class="mini-textbox" style="width:100%;"/></div>
		<div field="desc" width="100">描述<input property="editor" class="mini-textbox" style="width:100%;"/></div>
		<div field="updated" dateFormat="yyyy-MM-dd" width="100">更新时间</div>
		<div name="action" width="120" headerAlign="center" align="center" renderer="onActionRenderer" cellStyle="padding:0;">操作</div>
	</div>
</div>
<script type="text/javascript">
// 画面初期化
mini.parse();
var urlDel = "/basic/positionDel";
var urlUpd = "/basic/positionUpd?agency=0";
var grid = mini.get("grid1");
var treegrid = mini.get("treegrid1");
var rowid = 0;
treegrid.expandAll();
treegrid.on("nodeselect",function (e) {
	var rows = treegrid.getSelectedNode();
	grid.loading("加载中，请稍后......");
	mini.get("buttonAdd").enable();
	if(rows != null){
		rowid = rows.id;
		urlUpd = "/basic/positionUpd?agency=" + rows.id;
		grid.load({agency:rows.id});
	}
});

$("#agencyReload").click(function(){
	treegrid.load("/common/agency");
	grid.load({agency:0});
});

$("#agencyAdd").click(function(){
	var row = treegrid.getSelectedNode();
	if (row != null) {
		mini.open({
            url: "/basic/agencyEdit",
            title: "新增组织机构", width: 500, height: 250,
            onload: function()
            {
                var iframe = this.getIFrameEl();
                var data = { action: "new",id:row.id};
                iframe.contentWindow.SetData(data);
            },
            ondestroy:function(action)
            {
            	treegrid.load("/common/agency");
            	treegrid.expandAll();
            }
        });
		$(".mini-panel-border .mini-panel-body iframe").css("height","95%");
	} else {
		parent.$.dooDialog.alert("请选中一条记录");
	}
});

$("#agencyEdit").click(function(){
	var row = treegrid.getSelectedNode();
	if (row != null) {
		mini.open({
            url: "/basic/agencyEdit",
            title: "修改组织机构", width: 500, height: 250,
            onload: function()
            {
                var iframe = this.getIFrameEl();        		
                var data={action:"edit",id:row.id,types:row.types,name:row.name,businesstext:row.businessname,business:row.business,addres:row.addres,contact:row.contact,status:row.status,tel:row.tel,desc:row.desc};
                iframe.contentWindow.SetData(data);
            },
            ondestroy:function(action)
            {
            	treegrid.load("/common/agency");
            	treegrid.expandAll();
            }
        });
		$(".mini-panel-border .mini-panel-body iframe").css("height","95%");
	} else {
		parent.$.dooDialog.alert("请选中一条记录");
	}
});

$("#agencyDel").click(function(){
	var row = treegrid.getSelectedNode();
	if (row != null) {
		if(row.id != 1 && row.parent != 1){
			parent.$.dooDialog.confirm("你确定要删除这组织机构吗？",function(){
				$.post('/basic/agencyDel',{id:row.id},function(data){
					if(data.code=='OK'){
						treegrid.removeNode(row);
					}else if(data.code=='GO'){
						parent.$.dooDialog.alert('这个组织机构已经被使用不能删除，请确认！');
						return false;
					}else{
						parent.$.dooDialog.alert('这个组织机构删除失败！');
						return false;
					}
				},'json');
			});
		}else{
			parent.$.dooDialog.alert("不能删除总公司直属组织机构！");
		}
	} else {
		parent.$.dooDialog.alert("请选中一条记录");
	}
});
$("#positionReload").click(function(){
	grid.load({agency:rowid});
});
</script>
